package com.icss.ah.shop.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class JfShoppingService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	/**
	 * 查询店铺基本信息
	 * @param paraMap
	 * @return
	 */
	public Map<String,Object> getShopBasicInf(Map<String,Object> paraMap) {
		Map<String,Object> rtnMap = null;
		try{
			rtnMap = sqlSessionTemplate.selectOne("JfShoppingBean.getShopBasicInf", paraMap);
			infolog.info("JfShoppingService|getShopBasicInf|rtnMap:" + rtnMap);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("JfShoppingService|getShopBasicInf", e);
		}
		return null == rtnMap ? new HashMap<String,Object>() : rtnMap;
	}
	/**
	 * 查询访问人数
	 * @param paraMap
	 * @return
	 */
	public Map<String,Object> getVisitShopInf(Map<String,Object> paraMap) {
		List<Map<String,Object>> rtnList = null;
		Map<String,Object> rtnMap = new HashMap<String,Object>(3);
		long visitNum = 0;//访问次数
		try{
			rtnList = sqlSessionTemplate.selectList("JfShoppingBean.getVisitShopInf", paraMap);
			if(null != rtnList) {
				for (Map<String, Object> map : rtnList) {
					long cnt = null == map.get("VISITNUM") ? 0 : Long.parseLong(map.get("VISITNUM").toString());
					visitNum += cnt;
				}
			}
			infolog.info("JfShoppingService|getOrderInf|visitNum:" + visitNum);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("JfShoppingService|getOrderInf", e);
		}
		rtnMap.put("visitNum", visitNum);
		return rtnMap;
	}
	
	/**
	 * 最近7天订单评价汇总信息
	 * @param paraMap
	 * @return
	 */
	public List<Map<String,Object>> get7DaysOrderInf(Map<String,Object> paraMap) {
		List<Map<String,Object>> rtnList = null;
		try{
			rtnList = sqlSessionTemplate.selectList("JfShoppingBean.get7DaysOrderInf", paraMap);
			
			infolog.info("JfShoppingService|get7DaysOrderInf|rtnList:" + rtnList);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("JfShoppingService|get7DaysOrderInf", e);
		}
		return rtnList;
	}
	
	/**
	 * 最近一个月订单评价汇总信息
	 * @param paraMap
	 * @return
	 */
	public List<Map<String,Object>> getMonthDaysOrderInf(Map<String,Object> paraMap) {
		List<Map<String,Object>> rtnList = null;
		try{
			rtnList = sqlSessionTemplate.selectList("JfShoppingBean.getMonthDaysOrderInf", paraMap);
			infolog.info("JfShoppingService|getMonthDaysOrderInf|rtnList:" + rtnList);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("JfShoppingService|getMonthDaysOrderInf", e);
		}
		return rtnList;
	}
	
	/**
	 * 最近半年订单评价汇总信息
	 * @param paraMap
	 * @return
	 */
	public List<Map<String,Object>> getHalfYearDaysOrderInf(Map<String,Object> paraMap) {
		List<Map<String,Object>> rtnList = null;
		try{
			rtnList = sqlSessionTemplate.selectList("JfShoppingBean.getHalfYearDaysOrderInf", paraMap);
			infolog.info("JfShoppingService|getHalfYearDaysOrderInf|rtnList:" + rtnList);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("JfShoppingService|getHalfYearDaysOrderInf", e);
		}
		return rtnList;
	}
	
	/**
	 * 最近一年订单评价汇总信息
	 * @param paraMap
	 * @return
	 */
	public List<Map<String,Object>> getYearDaysOrderInf(Map<String,Object> paraMap) {
		List<Map<String,Object>> rtnList = null;
		try{
			rtnList = sqlSessionTemplate.selectList("JfShoppingBean.getYearDaysOrderInf", paraMap);
			infolog.info("JfShoppingService|getYearDaysOrderInf|rtnList:" + rtnList);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("JfShoppingService|getYearDaysOrderInf", e);
		}
		return rtnList;
	}
	
	/**
	 * 最近订单数量
	 * @param paraMap
	 * @return
	 */
	public Map<String,Object> getOrderByDate(Map<String,Object> paraMap) {
		List<Map<String,Object>> rtnList = null;
		Map<String,Object> orderMap = null;
		try{
			rtnList = sqlSessionTemplate.selectList("JfShoppingBean.getOrderByDate", paraMap);
			if(null != rtnList) {
				orderMap = new HashMap<String,Object>(5);
				for (Map<String, Object> map : rtnList) {
					int type = Integer.parseInt(map.get("ORDERTYPE").toString());
					if(type == 1) {//最近7天
						orderMap.put("LAST7DAYCNT", map.get("LAST7DAYCNT"));
					} else if(type == 2) { //最近一个月
						orderMap.put("LASTMONTHCNT", map.get("LASTMONTHCNT"));
					} else if(type == 3) { //最近半年
						orderMap.put("LASTHALFYEARCNT", map.get("LASTHALFYEARCNT"));
					} else if(type == 4) { //最近一年
						orderMap.put("LASTYEARCNT", map.get("LASTYEARCNT"));
					}
				}
			}
			infolog.info("JfShoppingService|getOrderByDate|rtnList:" + rtnList);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("JfShoppingService|getYearDaysOrderInf", e);
		}
		return orderMap;
	}
	
	/**
	 * 商品销售汇总分析
	 * @param paraMap
	 * @return
	 */
	public Map<String,Object> getOrderDetailInf(Map<String,Object> paraMap) {
		List<Map<String,Object>> list = null;
		Map<String,Object> rtnMap = new HashMap<String,Object>(10);
		long sumOrder = 0, //总订单
			completeOrder = 0, //已完成订单
			returnOrder = 0, //退货数量
			evaluateOrder = 0, //已评价
			priseCnt = 0, //好评率
			sumSellProductSort = 0; //销售总商品（种）
		double sumRealMoney = 0.00;//实付总金额
 		String sumRealMoneyStr = "0.00";
		try{
			list = sqlSessionTemplate.selectList("JfShoppingBean.getOrderDetailInf", paraMap);
			if(null != list) {
				for (Map<String, Object> map : list) {
					//订单状态 0：取消订单，1：等待买家付款，2：买家已付款，3：处理中，4：配送中，5：完成订单，6：已评价
					int orderStatus = Integer.parseInt(map.get("ORDER_STATUS").toString());
					int refund_count = Integer.parseInt(map.get("REFUND_COUNT").toString());
					long orderSum = Long.parseLong(null == map.get("SUMORDER") ? "0" :map.get("SUMORDER").toString());
					double money = Double.parseDouble(null == map.get("SUMREALMONEY") ? "0.00" :map.get("SUMREALMONEY").toString());
					String moneyStr = null == map.get("SUMREALMONEY") ? "0.00" :map.get("SUMREALMONEY").toString();
					sumOrder += orderSum;
					returnOrder += refund_count;
					BigDecimal x = new BigDecimal(moneyStr);
					BigDecimal y = new BigDecimal(sumRealMoneyStr);
					if(orderStatus>=2){
						y = x.add(y);
					}
					sumRealMoneyStr = y.toString();
					sumRealMoney = Double.valueOf(sumRealMoneyStr);
					if(orderStatus == 5) {
						completeOrder += orderSum;
					} else if(orderStatus == 6) {
						completeOrder += orderSum;
						long pCnt = Long.parseLong(map.get("PRISECNT").toString());
						evaluateOrder += orderSum;
						priseCnt += pCnt;
					}
				}
			}
			sumSellProductSort = sqlSessionTemplate.selectOne("JfShoppingBean.getOrderProductCount", paraMap);
			infolog.info("JfShoppingService|getOrderDetailInf|rtnMap:" + rtnMap);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("JfShoppingService|getOrderDetailInf", e);
		}
		rtnMap.put("sumOrder", sumOrder);
		rtnMap.put("completeOrder", completeOrder);
		rtnMap.put("returnOrder", returnOrder);
		rtnMap.put("evaluateOrder", evaluateOrder);
		rtnMap.put("priseCnt", priseCnt);
		rtnMap.put("sumSellProductSort", sumSellProductSort);
		rtnMap.put("sumRealMoney", sumRealMoney);
		return rtnMap;
	}
	
	
}
